<template>
	<view class="add_main">
		<view class="truth">
			<p class="text_top_popup">输入条码查询</p>
			<p class="tip">条码类型：SN码/一物一码</p>
			<uni-easyinput suffixIcon="scan" placeholderStyle="font-size:28rpx;"  @input="nextTick(()=>truthCode=truthCode.replace(/[\u4e00-\u9fa5]/g, ''))" v-model="truthCode" placeholder="请手动输入或扫描SN码/一物一码"
				@iconClick="iconClick"></uni-easyinput>
			<button class="onlogout" @click="addCode"><span>查询</span></button>
		</view>
	</view>
</template>

<script lang="ts" setup>
	import {
		ref,
		onMounted,
		nextTick
	} from "vue";
	import { onUnload } from '@dcloudio/uni-app'
	import apiCode from '@/util/network_api/code.js'
	const truthCode = ref('')//防伪码
	// 调起客户端扫码界面，扫码成功后返回对应的结果。
	const iconClick = () => {
		uni.scanCode({
			scanType: ['qrCode', 'barCode'],//扫码类型
			success: async function (res) {
				truthCode.value = res.result
				await addCode()
			},
			fail: (err) => {
				// #ifdef H5
				uni.showToast({
					icon: 'none',
					title: '设备不支持此功能',
					duration: 3000
				})
				// #endif
			}
		})
	}
	onUnload(() => {
			uni.hideLoading()
		})
	// 防伪查询
	const addCode = async () => {
		truthCode.value=truthCode.value.trim()
		if (truthCode.value) {
			uni.showLoading({
				title: "加载中...",
				mask: true
			})
			// 防伪查询 - 根据编码-仓库编码查询
			await apiCode.getBySnCode( truthCode.value).then(res => {
				if (res.code == 20000) {
					// 获取id
					uni.navigateTo({
						url: '/pages_network/home/truePage/truthPage?code=' + truthCode.value
					})
					// truthCode.value = ''
				}
			})
			uni.hideLoading()
		} else {
			uni.showToast({
				title: '请先输入条码',
				icon: 'none'
			})
		}
	}
</script>

<style lang="scss" scoped>
	.add_main {
		background-color: #f6f7fa;
	/* #ifdef H5 */
		height: calc(100vh - 44px);
		/* #endif */
		/* #ifndef H5 */
		height: 100vh;
		/* #endif */
		.truth {
			// background-color: #fff;
			border-radius: 20rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			padding: 40rpx;

			.text_top_popup {
				text-align: center;
				font-size: 32rpx;
				font-weight: bold;
			}

			.tip {
				font-size: 24rpx;
				color: #777;
				padding: 24rpx;
			}

			.onlogout {
				font-size: 28rpx;
				margin-top: 30rpx;
				height: 80rpx;
				border-radius: 50rpx;
				line-height: 80rpx;
				width: 100%;
				color: #fff;
				background-color: #007cff;
			}

			:deep(.is-input-border) {
				border-radius: 50rpx !important;
				// background-color: rgba(243, 244, 248, 1) !important;
				padding: 10rpx;
				margin: 20rpx 0 20rpx;
				border: none;
				height: 80rpx !important;
				line-height: 80rpx;
				font-size: 28rpx
			}
		}
	}
</style>